package com.t9101DR.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.t9101DR.bean.buy;
import com.t9101DR.bean.wish;
import com.t9101DR.dao.BaseDao;
import com.t9101DR.dao.buyDao;

public class buyDaoImpl extends BaseDao implements buyDao {

	@Override
	public List<buy> list(buy b) {
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List<buy> list=new ArrayList<buy>();
		try {
			conn=getConnection();
			String sql="SELECT buyId,buyName,buyAddress,buyphone,buyBuffId FROM `buy` WHERE 1=1";
			if(b!=null&&b.getBuyId()!=0) {
				sql=sql+" and buyId= "+b.getBuyId()+" ";
			}
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()) {
				buy bren=new buy();
				bren.setBuyId(rs.getInt(1));
				bren.setBuyName(rs.getString(2));
				bren.setBuyAddress(rs.getString(3));
				bren.setBuyphone(rs.getInt(4));
				bren.setBuyBuffld(rs.getInt(5));
				
				list.add(bren);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeAll(conn, pstmt, rs);
		}
		return list;
	}

	@Override
	public int add(buy b) {
		int count =0;
		String sql="INSERT INTO buy(buyName,buyAddress,buyphone,buyBuffId) VALUES (?,?,?,?) ";
		Object [] pare= {b.getBuyName(),b.getBuyAddress(),b.getBuyphone(),b.getBuyBuffld()};
		count=executeUpdate(sql, pare);
		return count;
	}

	@Override
	public int update(buy b) {
		int count =0;
		String sql=" UPDATE buy set buyName=?,buyAddress=?,buyphone=?,buyBuffId=? WHERE buyId=?  ";
		Object [] pare= {b.getBuyName(),b.getBuyAddress(),b.getBuyphone(),b.getBuyBuffld()};
		count=executeUpdate(sql, pare);
		return count;
	}

	@Override
	public int deleteById(int id) {
		int count=0;
		String sql="delete from order where buyId=?";
		Object[] params= {id};
		count=this.executeUpdate(sql, params);
		return count;
	}

}
